//
//  MCChangePasswordOneViewController.swift
//  StoreManage
//
//  Created by MC on 2018/8/29.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCChangePasswordOneViewController: MCViewController {


    public var fatherVC : UIViewController?   // 用来做present模态跳转
    public var phoneNumber = ""               // 传过来的手机号
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func codeButtonClicked() {
        
        guard phoneTextField.text?.count == 11 else {
            MCAlterController_oneOptions(vc: self, message: "请填写正确的手机号", cancle: { })
            return
        }
        
        self.codeTextField.cutDownButton.canRequest = true
        
        let para = [
            "userName" : phoneTextField.text ?? "",
            "codeType" : 2
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_sendCode, para: para as [String : AnyObject], vc: self,hudSuperView:self.view, isHiddenTabbar: true, success: { (response) in
            
            self.codeTextField.cutDownButton.isRequestSuccess = true
            
            MCHUD_text(message: "验证码发送成功", view: self.view, delay: 1.5)
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    
    @objc func usePasswordButtonClicked() {
        let vc = MCChangePasswordTwoViewController()
        vc.fatherVC = fatherVC
        vc.phoneNumber = phoneNumber
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    
    
    @objc func confirmButtonClicked() {
        
        guard (phoneTextField.text?.count == 11 && codeTextField.text.count != 0 && passwordTextField.text?.count != 0) else {
            MCAlterController_oneOptions(vc: self, message: "账号,者验证码或密码不正确", cancle: { })
            return
        }
        
        let newPassword = passwordTextField.text ?? ""
        
        let para = [
            "userName"    : phoneTextField.text ?? "",
            "code"        : codeTextField.text,
            "newPassword" : newPassword.MCMD5()
            ] as [String : Any]

        MCNetwork_post(url: JAVA_resetPassword, para: para as [String : AnyObject], vc: self,hudSuperView:self.view, isHiddenTabbar: true, success: { (response) in

            let dataDcit = response["data"] as? [String:Any] ?? [:]

            MCHUD_text(message: "修改密码成功", view: self.view, delay: 1)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                self.view.endEditing(true)
                let vc = MCChangeStoreViewController()
                vc.isLoginVCJump = true
                vc.userDict = dataDcit
                let nav = MCNavigationController.init(rootViewController: vc)
                self.fatherVC!.present(nav, animated: true, completion: nil)
            })

        }, dislike: { (response) in }) { (error) in }
    }
    
    
    // MARK: - Method
    func baseSetting() {
        title = "修改密码"
        
        phoneTextField.text = phoneNumber
    }
    
    
    func initUI() {
        
        view.addSubview(phoneTextField)
        phoneTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(25)
            make.height.equalTo(40)
        }
        
        view.addSubview(codeTextField)
        codeTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(phoneTextField.snp.bottom).offset(10)
            make.height.equalTo(40)
        }
        
        view.addSubview(passwordTextField)
        passwordTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(codeTextField.snp.bottom).offset(10)
            make.height.equalTo(40)
        }

        
        view.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(phoneTextField)
            make.top.equalTo(passwordTextField.snp.bottom).offset(20)
        }

        view.addSubview(usePasswordButton)
        usePasswordButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(promptLabel.snp.right)
            make.centerY.equalTo(promptLabel)
        }

        view.addSubview(confirmButton)
        confirmButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.top.equalTo(promptLabel.snp.bottom).offset(60)
            make.size.equalTo(CGSize.init(width: MCScreenWidth * 0.65, height: 40))
        }
        
        
        view.addSubview(promptTwoLabel)
        promptTwoLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.top.equalTo(confirmButton.snp.bottom).offset(20)
        }
        
        
    }
    
    // MARK: - Setter & Getter
    
    lazy var phoneTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入手机号码"
        textField.font = MCFont(14)
        textField.keyBoardType = MCKeyBorardType.Number
        return textField
    }()
    
    lazy var codeTextField: MCVerificationCodeTextField = {
        let tf = MCVerificationCodeTextField()
        weak var weakSelf = self
        tf.cutDownButton.clickedClosure = { () -> Void in
            weakSelf?.codeButtonClicked()
        }
        
        return tf
    }()
    
    lazy var passwordTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入新的登录密码"
        textField.font = MCFont(14)
        textField.keyBoardType = MCKeyBorardType.asciiCapable
        textField.isSecureTextEntry = true
        return textField
    }()

    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(13)
        label.textColor = UIColor.MCGray_middle
        label.text = "未收到验证码？"
        return label
    }()
    
    lazy var promptTwoLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(13)
        label.textColor = UIColor.MCGray_middle
        label.text = "如果您不记得当前密码，可联系店主重置密码"
        return label
    }()

    
    lazy var usePasswordButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(13)
        button.setTitle("试试输入原密码修改密码", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.addTarget(self, action: #selector(usePasswordButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

    
    
    lazy var confirmButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.layer.cornerRadius = 4
        button.layer.masksToBounds = true
        button.setTitle("完成", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(confirmButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
}
